package com.nondo.dean.platform.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.platform.domain.City;
import com.nondo.dean.platform.domain.CityExample;
import com.nondo.dean.platform.mapper.CityMapper;
import com.nondo.dean.platform.model.AreaBean;

/**
 * 城市管理业务类
 * 
 * @author zhaoljin
 * @date 2018年4月3日
 */
@Component
public class CityService {

    @Autowired
    private CityMapper cityMapper;

    /**
     * 插入城市
     * @param city
     * @return
     */
    @Transactional
    public int addCity(City city) {
        return cityMapper.insertSelective(city);
    }

    /**
     * 修改城市
     * @param city
     * @return
     */
    @Transactional
    public int updateCity(City city) {
        return cityMapper.updateByPrimaryKeySelective(city);
    }

    /**
     * 按照主键ID查询城市
     * @param id
     * @return
     */
    public City selectCity(Integer id) {
        return cityMapper.selectByPrimaryKey(id);
    }

    /**
     * 城市分页查询列表
     * @param offset
     * @param limit
     * @param city
     * @return
     */
    public List<City> getCityList(int offset, int limit, City city) {
        CityExample example = new CityExample();
        RowBounds rowBounds = new RowBounds(offset, limit);
        CityExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, city);
        return cityMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 城市总数统计
     * @param city
     * @return
     */
    public Long count(City city) {
        CityExample example = new CityExample();
        CityExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, city);
        return cityMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param city
     */
    void setCriteria(CityExample.Criteria criteria, City city) {
        if (city != null) {
            if (city.getId() != null)
                criteria.andIdEqualTo(city.getId());
            if (StringUtils.isNoneBlank(city.getProvinceCode())) {
                criteria.andProvinceCodeEqualTo(city.getProvinceCode());
            }
            if (StringUtils.isNoneBlank(city.getName()))
                criteria.andNameLike("%" + city.getName() + "%");
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

   /**
    * 按照城市名称查询城市
    * @param name
    * @return
    */
    public City selectCityByName(String name) {
        CityExample example = new CityExample();
        CityExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<City> cityList = cityMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(cityList)?cityList.get(0):null;
    }
    
    /**
     * 按照省份获取城市列表
     * @param code 省份代码
     * @return {@link City}
     */
    public List<AreaBean> getCityListByProviceCode(String code) {
        List<AreaBean> resultList = new ArrayList<AreaBean>();
        CityExample example = new CityExample();
        CityExample.Criteria criteria = example.createCriteria();
        criteria.andProvinceCodeEqualTo(code);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<City> cityList = cityMapper.selectByExample(example);
        for (City city : cityList) {
            AreaBean areaBean = new AreaBean();
            areaBean = AreaBean.convertToBean(city);
            resultList.add(areaBean);
        }
        return resultList;
    }
}
